package com.huaxin.hxcloud.find.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.activity.ReportActivity;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：ikkong （ikkong@163.com），修改 jeasonlzy（廖子尧）
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：微信图片选择的Adapter, 感谢 ikkong 的提交
 * ================================================
 */
public class ImagePickerAdapter extends RecyclerView.Adapter<ImagePickerAdapter.SelectedPicViewHolder> {
    private static final String TAG = "ImagePickerAdapter";
    private int                             maxImgCount;
    private Context                         mContext;
    private List<ImageItem>                 mData;
    private LayoutInflater                  mInflater;
    private OnRecyclerViewItemClickListener listener;
    private boolean                         isAdded;   //是否额外添加了最后一个图片
    private OnDeleteItemClickListener       mDelecteListener; //删除回调

    public interface OnDeleteItemClickListener {
        void onDeleteItemClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

    public void setDeleteItemClickListener(OnDeleteItemClickListener listener) {
        this.mDelecteListener = listener;
    }

    public void setImages(List<ImageItem> data) {
        mData = new ArrayList<>(data);
        if (getItemCount() < maxImgCount) {
            mData.add(new ImageItem());
            isAdded = true;
        } else {
            isAdded = false;
        }
        notifyDataSetChanged();
    }

    private boolean mIsUrl = false;

    public void setImagesUrl(List<ImageItem> data, boolean isUrl) {
        setImages(data);
        mIsUrl = isUrl;

    }

    public List<ImageItem> getImages() {
        //由于图片未选满时，最后一张显示添加图片，因此这个方法返回真正的已选图片
        if (isAdded)
            return new ArrayList<>(mData.subList(0, mData.size() - 1));
        else
            return mData;
    }

    public ImagePickerAdapter(Context mContext, List<ImageItem> data, int maxImgCount) {
        this.mContext = mContext;
        this.maxImgCount = maxImgCount;
        this.mInflater = LayoutInflater.from(mContext);
        setImages(data);
    }

    private boolean mIsEdit = true;

    public void setEdit(boolean isEdit) {
        this.mIsEdit = isEdit;
        notifyDataSetChanged();
    }

    @Override
    public SelectedPicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectedPicViewHolder(mInflater.inflate(R.layout.list_item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(SelectedPicViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class SelectedPicViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_img, deldet_img;
        private int clickPosition;

        public SelectedPicViewHolder(View itemView) {
            super(itemView);
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
            deldet_img = (ImageView) itemView.findViewById(R.id.delete);
        }

        public void bind(int position) {
            //设置条目的点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null)
                        listener.onItemClick(view, clickPosition);
                }
            });


            //删除图片的点击事件
            deldet_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mDelecteListener != null)
                        mDelecteListener.onDeleteItemClick(clickPosition);
                }
            });
            //根据条目位置设置图片
            final ImageItem item = mData.get(position);
            if (isAdded && position == getItemCount() - 1) {
                iv_img.setImageResource(R.drawable.selector_image_add);
                clickPosition = ReportActivity.IMAGE_ITEM_ADD;
                deldet_img.setVisibility(View.GONE);
            } else {

                if (mIsEdit) {//编辑状态
                    deldet_img.setVisibility(View.VISIBLE);
                } else {
                    deldet_img.setVisibility(View.GONE);
                }
                if (!mIsUrl) {
                    ImagePicker.getInstance().getImageLoader().displayImage((Activity) mContext, item.path, iv_img, 0, 0);
                } else {
                    specialImage((Activity) mContext, item.path, iv_img);
                }


                clickPosition = position;
            }

        }
    }

    public void specialImage(Activity activity, String url, ImageView imageView) {
        Glide.with(activity).load(url).apply(new RequestOptions().error(R.drawable.default_image).placeholder(R.drawable.default_image)
        .diskCacheStrategy(DiskCacheStrategy.ALL)).into(imageView);
        /*GlideApp.with(activity)                             //配置上下文
                .load(url)      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                .error(R.drawable.default_image)           //设置错误图片
                .placeholder(R.drawable.default_image)     //设置占位图片
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .into(imageView);*/
    }
}